package com.zige.zige.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.zige.zige.R;
import com.zige.zige.utils.ACache;
import com.zige.zige.utils.NetworkUtils;
import com.zige.zige.utils.SharedPreferencesUtils;


public class Welcome2Activity extends Activity {

    private ImageView welcome2;
    private View rootView;
    private ACache mCache;
    private Thread playVoice;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//            enterHome();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏2222222222
        setContentView(R.layout.activity_welcome2);
        mCache = ACache.get(this);
        welcome2 = (ImageView) findViewById(R.id.welcome2);
        initView();
        handler.sendEmptyMessageDelayed(1, 100);

        playVoice = new Thread(myPlay);
        playVoice.start();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {

        }

    }

    private void initView() {
        rootView = findViewById(R.id.ly_welcome2);
        welcome2 = (ImageView) findViewById(R.id.welcome2);
        String urlkey = mCache.getAsString("OpenClientImage");
        Bitmap bitmap = mCache.getAsBitmap(urlkey);
        if (bitmap != null) {
            welcome2.setImageBitmap(bitmap);
        }

    }


    private void enterHome() {
        // AnimationSet as =new AnimationSet(this);
        Bundle bundle = getIntent().getExtras();
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(this, HomedActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.home_in, R.anim.home_out);
    }

    private Runnable myPlay = new Runnable() {
        @Override
        public void run() {

            playMusic();
        }
    };

    private void playMusic() {
        String voiceUrl = (String) SharedPreferencesUtils.getValue(this, "voiceUrl", "");
        if (NetworkUtils.isNetworkAvailable(Welcome2Activity.this)) {
            if (voiceUrl != null && voiceUrl != "") {
                MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse(voiceUrl));
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        enterHome();
                        SharedPreferencesUtils.saveValue(Welcome2Activity.this,"voiceUrl",null);
                    }
                });
            } else {
                MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.ding);
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        enterHome();
                    }
                });
            }
        } else {
            MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.ding);
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    enterHome();
                }
            });
        }


    }


}